import allure
from selenium.webdriver.common.by import By

from cloud_po.pages.base_page import BasePage
from cloud_po.utils.log_utils import logger


class HomePage(BasePage):

    __WAREHOUSE_MANGE_BTN = (By.XPATH,"//*[text()='仓库(供应商)管理']")
    __WAREHOUSE_BTN = (By.XPATH,"//*[text()='仓库(供应商)']")
    __CHANNEL_MANGE_BTN = (By.XPATH, "//*[text()='渠道(群)']")
    __CHANNEL_BTN = (By.XPATH, "//*[text()='渠道管理']")
    __PRODUCT_MANGE_BTN = (By.XPATH, "//*[text()='商品管理']")
    __PRODUCT_BTN = (By.XPATH, "//*[text()='商品列表']")
    __ORDER_MANGE_BTN = (By.XPATH, "//*[text()='订单管理']")
    __ORDER_ENTRY_BTN = (By.XPATH, "//*[text()='订单录入']")
    __ORDER_LIST_BTN = (By.XPATH, "//*[text()='订单列表']")

    def click_warehouse_manage(self):
        logger.info("进入首页,点击仓库（供应商）导航栏")
        with allure.step("点击仓库(供应商)管理-仓库(供应商)"):
            # 判断如果是展开的就不用点击外层了，否则点击
            class_name = self.do_find(By.XPATH, "//*[text()='仓库(供应商)管理']/../..").get_attribute("class")
            if "is-opened" not in class_name :
                self.do_find(self.__WAREHOUSE_MANGE_BTN).click()
            self.do_find(self.__WAREHOUSE_BTN).click()

        logger.info("返回仓库页面")
        from cloud_po.pages.warehouse_page import WareHousePage
        return WareHousePage(self.driver)

    def click_channel_manage(self):
        logger.info("进入首页,点击渠道管理导航栏")
        with allure.step("点击渠道（群）-渠道管理"):
            # 判断如果是展开的就不用点击外层了，否则点击
            class_name = self.do_find(By.XPATH, "//*[text()='渠道(群)']/../..").get_attribute("class")
            if "is-opened" not in class_name :
                self.do_find(self.__CHANNEL_MANGE_BTN).click()
            self.do_find(self.__CHANNEL_BTN).click()

        logger.info("返回渠道管理页面")
        from cloud_po.pages.channel_page import ChannelPage
        return ChannelPage(self.driver)


    def click_product_manage(self):
        logger.info("进入首页,点击商品列表导航栏")
        with allure.step("点击商品管理-商品列表"):
            class_name = self.do_find(By.XPATH, "//*[text()='商品管理']/../..").get_attribute("class")
            if "is-opened" not in class_name :
                self.do_find(self.__PRODUCT_MANGE_BTN).click()
            self.do_find(self.__PRODUCT_BTN).click()

        logger.info("返回商品列表页面")
        from cloud_po.pages.product_page import ProductPage
        return ProductPage(self.driver)

    def click_order_entry(self):
        logger.info("进入首页,点击订单录入导航栏")
        with allure.step("点击订单管理-订单录入"):
            class_name = self.do_find(By.XPATH, "//*[text()='订单管理']/../..").get_attribute("class")
            if "is-opened" not in class_name:
                self.do_find(self.__ORDER_MANGE_BTN).click()
            self.do_find(self.__ORDER_ENTRY_BTN).click()

        logger.info("返回商品列表订单录入页面")
        from cloud_po.pages.order_entry_page import OrderEntryPage
        return OrderEntryPage(self.driver)


    def click_order_list(self):
        logger.info("进入首页,点击订单列表导航栏")
        with allure.step("点击订单管理-订单列表"):
            class_name = self.do_find(By.XPATH, "//*[text()='订单管理']/../..").get_attribute("class")
            if "is-opened" not in class_name:
                self.do_find(self.__ORDER_MANGE_BTN).click()
            self.do_find(self.__ORDER_LIST_BTN).click()

        logger.info("返回商品列表订单列表页面")
        from cloud_po.pages.order_page import OrderPage
        return OrderPage(self.driver)



